package com.jinyuan.options.login;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;

import com.android.threadpool.IActivity;
import com.android.threadpool.Task;
import com.jinyuan.R;
import com.jinyuan.options.OptionsFragment;
import com.jinyuan.request.RequestURL;
import com.jinyuan.service.MobileApplication;
import com.jinyuan.service.ParentHandlerService;
import com.jinyuan.service.TaskID;
import com.jinyuan.view.NewFontsEditText;

/**
 * 用户注册
 * 
 * @author azuryleaves
 * @since 2014-1-3 下午12:14:13
 * @version 1.0
 * 
 */
public class RegisterFragment extends OptionsFragment implements IActivity {

	private Button parentdialog_confirm_Button;
	private View view;
	private NewFontsEditText account_EditText, password_EditText,
			passwordagain_EditText;
	private Animation shake;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		MobileApplication.allIActivity.add(this);
		view = inflater.inflate(R.layout.login_tab2, container, false);
		parentdialog_confirm_Button = (Button) ((ViewGroup) container
				.getParent()).findViewById(R.id.parentdialog_confirm_Button);
		parentdialog_confirm_Button.setOnClickListener(clickListener);
		init();
		return view;
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.parentdialog_confirm_Button:
				if (checkEmpty()) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put(ParentHandlerService.URL, RequestURL.getRegister());
					map.put("username", account_EditText.getText().toString()
							.trim());
					map.put("password", password_EditText.getText().toString()
							.trim());
					Task task = new Task(TaskID.TASK_REGISTER, map, this
							.getClass().getName(), "-用户注册-");
					loadingDialog.show();
					MobileApplication.poolManager.addTask(task);
				}
				break;
			default:
				break;
			}
		}
	};

	private boolean checkEmpty() {
		if ("".equals(account_EditText.getText().toString().trim())) {
			Toast.makeText(getActivity(), R.string.account, Toast.LENGTH_SHORT)
					.show();
			account_EditText.startAnimation(shake);
		} else {
			if ("".equals(password_EditText.getText().toString().trim())) {
				Toast.makeText(getActivity(), R.string.password,
						Toast.LENGTH_SHORT).show();
				password_EditText.startAnimation(shake);
			} else {
				if ("".equals(passwordagain_EditText.getText().toString()
						.trim())) {
					Toast.makeText(getActivity(), R.string.password_again,
							Toast.LENGTH_SHORT).show();
					passwordagain_EditText.startAnimation(shake);
				} else {
					if (password_EditText
							.getText()
							.toString()
							.trim()
							.equals(passwordagain_EditText.getText().toString()
									.trim())) {
							return true;
					} else {
						Toast.makeText(getActivity(), R.string.password_error,
								Toast.LENGTH_SHORT).show();
						passwordagain_EditText.startAnimation(shake);
					}
				}
			}
		}
		return false;
	}

	@Override
	public void init() {
		account_EditText = (NewFontsEditText) view
				.findViewById(R.id.login_tab2_account_EditText);
		password_EditText = (NewFontsEditText) view
				.findViewById(R.id.login_tab2_password_EditText);
		passwordagain_EditText = (NewFontsEditText) view
				.findViewById(R.id.login_tab2_passwordagain_EditText);
		shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
	}

	@Override
	public void refresh(int taskID, Object... params) {
		loadingDialog.cancel();
		if (params != null) {
			if (params[0] != null) {
				try {
					JSONObject jsonObject = new JSONObject((String) params[0]);
					boolean success = jsonObject.getBoolean("success");
					System.out.println("---success---" + success);
					Toast.makeText(getActivity(),
							jsonObject.getString("message"), Toast.LENGTH_SHORT)
							.show();
					if (success) {
						getActivity().finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				loadingDialog.showTimeOut();
			}
		} else {
			loadingDialog.showTimeOut();
		}
	}

	@Override
	public void closeLoadingView() {
		loadingDialog.cancel();
	}

}